package com.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.exception.LMSException;
import com.interfaces.EmployeeManager;
import com.manager.EmployeeManagerImpl;
import com.view.EmployeePageView;

/**
 * Servlet implementation class AdminServlet.
 * 
 * @author : gbs02399 Created on : Jul 6, 2012 Project : LMS File Name :
 *         AdminServlet.java
 */
public class AdminServlet extends HttpServlet {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/**
	 * Instantiates a new admin servlet.
	 * 
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Do get.
	 * 
	 * @param request
	 *            the request
	 * @param response
	 *            the response
	 * @throws ServletException
	 *             the servlet exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		doPost(request, response);
	}

	/**
	 * Do post.
	 * 
	 * @param request
	 *            the request
	 * @param response
	 *            the response
	 * @throws ServletException
	 *             the servlet exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		final String action = request.getParameter("MODO");
		final EmployeeManager employeeManager = new EmployeeManagerImpl();
		System.out.println(action);
		if (action.equals("default")) {
			final EmployeePageView pageView = clearForm();
			request.setAttribute("employeePageView", pageView);
		}
		if (action.equals("formSubmit")) {
			final String empName = request.getParameter("empName");
			final String userName = request.getParameter("userName");
			final String password = request.getParameter("password");
			final String empRole = request.getParameter("empRole");
			final EmployeePageView pageView = new EmployeePageView();
			pageView.setEmpName(empName);
			pageView.setEmpUserName(userName);
			pageView.setEmpPassword(password);
			pageView.setEmpRole(empRole);
			System.out.println(pageView.getBean().toString());
			try {
				employeeManager.insertEmployee(pageView.getBean());
				request.setAttribute("employeePageView", clearForm());
				request.setAttribute("Message", "Employee Created Successfully");
			} catch (final LMSException e) {
				e.printStackTrace();
				request.setAttribute("employeePageView", pageView);
				request.setAttribute("error", e.getMessage());
			}

		}
		final RequestDispatcher reqDispatch = getServletContext()
				.getRequestDispatcher("/CreateEmployee.jsp");
		reqDispatch.forward(request, response);
	}

	/**
	 * Clear form.
	 * 
	 * @return the employee page view
	 */
	private EmployeePageView clearForm() {
		final EmployeePageView pageView = new EmployeePageView();
		pageView.setEmpName("");
		pageView.setEmpUserName("");
		pageView.setEmpPassword("");
		pageView.setEmpRole("");
		return pageView;
	}

}
